import rhythmdb, rb
import gtk

# Setup the item for the right click entry menu and the Edit menu
ui_str = """
<ui>
  	<popup name="BrowserSourceViewPopup">
		<placeholder name="PluginPlaceholder">
		      <menuitem name="OpenFolderPopup" action="QueueAlbum"/>
	   </placeholder>
	</popup>
	<popup name="QueuePlaylistViewPopup">
	   <placeholder name="PluginPlaceholder">
      	<menuitem name="OpenFolderPopup" action="QueueAlbum"/>
	   </placeholder>
	</popup>
	<menubar name="MenuBar"> 
		<menu name="EditMenu" action="Edit">
			<menuitem name="EditQueueAlbumMenu" action="QueueAlbum"/>
		</menu>
	</menubar>
</ui>
"""

class QueueAlbum(rb.Plugin):

	def __init__(self):
		rb.Plugin.__init__(self)
	
	# Set up the action, add it to the ui manager, add menu items
	def activate(self, shell):
		self.action = gtk.Action('QueueAlbum', _('Queue Album'), _('Queue Album....'), '')
		self.activate_id = self.action.connect('activate', self.queue_album, shell)
		action_group = gtk.ActionGroup ('NewActionGroup')
		action_group.add_action (self.action)
		shell.get_ui_manager().insert_action_group (action_group)
		ui_manager = shell.get_ui_manager()
		ui_manager.add_ui_from_string (ui_str)
		ui_manager.ensure_update()

	# Right click on song, get album, query all songs in that album, queue.
	def queue_album(self, action, shell): 
		source = shell.get_property("selected_source")
		entry = rb.Source.get_entry_view(source)
		selected = entry.get_selected_entries()
		db = shell.get_property ("db")
		self.album = db.entry_get (selected[0], rhythmdb.PROP_ALBUM)
		q = db.query_new()
		db.query_append(q, [rhythmdb.QUERY_PROP_EQUALS, rhythmdb.PROP_ALBUM, self.album])
		#qm = db.query_model_new(q)
		qm = db.query_model_new(q, rhythmdb.rhythmdb_query_model_track_sort_func, 0, db.query_model_new_empty())
		db.do_full_query_parsed(qm, q)

		for row in qm:
			entry = row[0]
			uri = entry.get_playback_uri()
			shell.add_to_queue(uri)

#print db.entry_get(entry, rhythmdb.PROP_TITLE)


#	q = self.db.query_new()
#	self.db.query_append(q, [rhythmdb.QUERY_PROP_EQUALS, rhythmdb.PROP_ALBUM, album_name])
#	qm = self.db.query_model_new(q)
#	self.db.do_full_query_parsed(qm, q)
#	self.library_source.get_entry_view().set_model(qm)
#	self.library_source.props.query_model = qm
#return True
	
# self.type_song=self.db.entry_type_get_by_name("song")

#def deactivate(self, shell):
		#self.album_view.disconnect(self.signal_id)
		#del self.db
		#del self.library_source
		#del self.album_view

